<script type="text/javascript">
var opkg_timeout;

$$(document).ready(function() {
    opkg_list_upgradable();
    $$('#check-all').click(function(){
        $$('.check-package').prop("checked", true);
    });
    $$('#check-none').click(function(){
        $$('.check-package').prop("checked", false);
    });
    $$('#install-now').click(function(){
        opkg_install();
    });
    $$('#check-for-updates').click(function(){
        opkg_update();
    });
});

function opkg_last_updated(){
    $$.post( "/ajax", { 
        'func': "opkg_last_updated"
    }).done(function( data ) {
        $$("#opkg-last-updated").html("Last updated: "+data.last_updated)
    });
}

function opkg_update(){
    $$("#opkg-spinner").css("display", "block");
    $$("#opkg-status").html("Checking for updates");
    $$.post( "/ajax", { 
        'func': "opkg_update"
    }).done(function( data ) {
        $$("#opkg-spinner").css("display", "none");
        $$("#opkg-last-updated").html("Last updated: "+data.last_updated)
        $$("#opkg-status").html("Getting upgradable packages");
        opkg_list_upgradable();
    });
}

function opkg_list_upgradable(){    
    $$("#opkg-spinner").css("display", "block");
    $$("#opkg-status").html("Getting upgradable packages")
    $$("#opkg-packages").css("display", "none");
    $$.post( "/ajax", { 
        'func': "opkg_list_upgradable"
    }).done(function( data ) {
        $$("#opkg-spinner").css("display", "none");
        if(data.ok){
            $$("#opkg-last-updated").html("Last updated: "+data.last_updated);
            if(data.packages_available){
                $$(".opkg-node").remove();
                $$("#opkg-status").html("Updates available for installation")
                $$("#opkg-packages").css("display", "block");
                for(var i in data.opkgs){
                    $$("#opkg").append(make_opkg(data.opkgs[i]))
                    $$("#opkg").append(make_opkg_info(data.opkgs[i]))
                }
            }
            else{
                $$("#opkg-status").html("No new updates available")
            }
        }
        else{
            $$("#opkg-status").html("Error getting packages")
        }
    });
}

function opkg_install(){
    $$("#opkg-status").html("Upgrading selected packages")
    $$("#opkg-spinner").css("display", "block");
    to_upgrade = []
    $$('.check-package:checked').each(function(){
        to_upgrade.push($$(this).attr("package"));
    })
    opkg_timeout = setTimeout('opkg_check_log(true)', 1000);
    $$.post( "/ajax", { 
        'func': "opkg_install",
        'packages': JSON.stringify(to_upgrade)
    }).done(function( data ) {
        if(data.ok){
            $$("#opkg-status").html("Upgrade completed")
        }
        else{
            $$("#opkg-status").html("Upgrade failed")
        }
        clearTimeout(opkg_timeout);
        opkg_check_log(false); // Check a final time
        opkg_list_upgradable(); // Refresh list of packages
        $$("#opkg-spinner").css("display", "none");
    }).fail( function(xhr, textStatus, errorThrown) {
        clearTimeout(opkg_timeout);
        opkg_check_log(false); // Check a final time
        opkg_list_upgradable();
        $$("#opkg-status").html("Upgrade error :(")
    });
}

function opkg_check_log(cont){
    $$.post( "/ajax", { 
        'func': "opkg_check_log"
    }).done(function( data ) {
        $$("#opkg-log").html(data.install_log)
        if(cont){
            opkg_timeout = setTimeout('opkg_check_log(true)', 1000);
        }
    });
}

function opkg_check_status(){
    $$.post( "/ajax", { 
        'func': "opkg_check_status"
    }).done(function( data ) {
        $$("#opkg-status").html(data.install_log)
    });
}

function make_opkg_info(opkg){
    info = $$("<tr package='"+opkg.name+"'><td colspan='2'>Getting package info...</td></tr>")
    info.css("display", "none")
    return info
}

function make_opkg(opkg){
    var tr = $$("<tr></tr>")
    tr.attr("class", "opkg-node")
    tr.append('<td><input type="checkbox" checked="1" class="check-package" package="'+opkg.name+'" autocomplete="off"/></td>')
    var name = $$("<td>"+opkg.name+"</td>")
    name.css("cursor", "pointer");
    name.click(function(){    
        var panel = $$(this).parent().next();
        var isOpen = panel.is(':visible');
        if(isOpen){
            panel.slideUp();
        }
        else{
            panel.slideDown();
            panel.html("<td colspan='2'>Getting package info...</td>");
            $$.post( "/ajax", { 
                'func': "opkg_info", 
                'name': panel.attr("package")
            }).done(function( data ) {
                panel.html("<td colspan='2'><pre>"+data.info+"</pre></td>")
            });
        }

        // stop the link from causing a pagescroll
        return false;
    })
    tr.append(name);    
    return tr
}
</script>

<h2>Package manager</h2>
<h3 id="opkg-status">Getting upgradable packages</h3> 
<p id="opkg-last-updated"></p>
<img id="opkg-spinner" src="static/images/spinner_squares_circle.gif" style="visibility: block; margin-left: auto; margin-right: auto">
<div id="opkg-packages" style="display: none">
    <div  style="max-height: 300px; border: 1px solid #ccc; overflow-y: auto;">
        <table id="opkg" style="width: 100%;">
            <tr>
                <th style="width: 20px;">Install</th>
                <th>Package</th>
            </tr>
        </table>
    </div>
    <pre id="opkg-log" style="visibilty: hidden; max-height: 200px; overflow-y: auto;">
    </pre>
    <div>
        <button type="button" id="check-all">Check all</button>
        <button type="button" id="check-none">Check None</button>
        <button type="button" id="install-now">Upgrade now</button>
    </div>
</div>
<button type="button" id="check-for-updates">Check for updates</button>

